package com.guomao.function;

import java.util.ArrayList;
import java.util.function.Predicate;

public class PredicateDemo {

    public static void main(String[] args) {

        String s = "Hello World";

        boolean flag1 = predicateString1(s, (str) -> {
            return str.length() > 5;
        });

        System.out.println(flag1);
        boolean flag2 = predicateString2(s, (str) -> {
            return str.length() > 5;
        });

        System.out.println(flag2);

        boolean flag3 = predicateString3(s, (s1) -> {
            return s1.contains("orld");
        }, (s2) -> {
            return s2.length() > 5;
        });

        System.out.println(flag3);


        String[] names = {"root,30", "admin,50", "lisi,65", "zhangsan,65"};

/*        ArrayList<String> lists = predicateNameArr(names, (str1) -> {
            String[] arr = str1.split(",");
            String name = arr[0];
            return name.length() > 4;
        }, (str2) -> {
            String[] arr = str2.split(",");
            String age = arr[1];
            int i = Integer.parseInt(age);
            return i > 40;
        });*/

        ArrayList<String> lists = predicateNameArr(names,
                (str1) -> str1.split(",")[0].length() > 4,
                (str2) -> Integer.parseInt(str2.split(",")[1]) > 40);
        System.out.println(lists);
    }


    public static boolean predicateString1(String str, Predicate<String> p) {
        boolean flag = p.test(str);
        return flag;
    }


    public static boolean predicateString2(String str, Predicate<String> p) {
//        return p.negate().test(str);
        return !p.test(str);
    }

    public static boolean predicateString3(String str, Predicate<String> p1, Predicate<String> p2) {
        return p1.and(p2).test(str);
    }

    public static ArrayList<String> predicateNameArr(String[] names,
                                                     Predicate<String> p1, Predicate<String> p2) {

        ArrayList<String> arrayList = new ArrayList<String>();
        for (String name : names) {
//            boolean flag = ;
            if (p1.and(p2).test(name)) {
                arrayList.add(name);
            }
        }

        return arrayList;
    }
}
